Skip to content

feat(prefer-use-template-ref): support auto-fix#2983

Draft
9romise wants to merge 10 commits intovuejs:masterfrom
9romise:prefer-template-ref/auto-fix
Draft

feat(prefer-use-template-ref): support auto-fix#2983
9romise wants to merge 10 commits intovuejs:masterfrom
9romise:prefer-template-ref/auto-fix

Conversation

@9romise
Copy link
Contributor

@9romise 9romise commented Nov 27, 2025

Re-implement of #2632 (closes #2632). (I really love this feature!)
But this PR doesn't insert import { useTemplateRef } from 'vue' automatically.
Because I think that falls outside the scope of this rule. Additionally, some users rely on unplugin-auto-import and would have to remove the unnecessary import to maintain clean imports.

Please merge #2982 first.

@changeset-bot
Copy link

changeset-bot bot commented Nov 27, 2025

🦋 Changeset detected

Latest commit: e07e5be

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-vue Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@9romise 9romise marked this pull request as ready for review November 28, 2025 01:27
Copy link
Member

@FloEdelmann FloEdelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you 🙂

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements auto-fix functionality for the prefer-use-template-ref rule, which automatically converts ref() and shallowRef() calls to useTemplateRef('name') when they are used as template refs. The implementation intentionally does not auto-import useTemplateRef to avoid conflicts with users who rely on unplugin-auto-import.

  • Adds fixable: 'code' metadata to enable auto-fixing
  • Implements fixer logic that correctly handles TypeScript type arguments/parameters
  • Updates all test cases with expected outputs and enhanced error location information

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
lib/rules/prefer-use-template-ref.js Adds fixable metadata and implements the fix function that replaces ref/shallowRef calls with useTemplateRef, preserving type arguments
tests/lib/rules/prefer-use-template-ref.js Adds output expectations for all invalid test cases and includes endLine/endColumn in error assertions; adds TypeScript test case
docs/rules/prefer-use-template-ref.md Adds wrench icon notation and fix attribute to code blocks to indicate auto-fix capability
docs/rules/index.md Updates the rules table to show the wrench icon for this rule

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@ota-meshi ota-meshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR!

</template>
<script setup>
import { ref } from 'vue';
const root = useTemplateRef('root');
Copy link
Member

@ota-meshi ota-meshi Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think autofixes shouldn't break source code.
I think adding import { useTemplateRef } from 'vue' will inconvenience users who use unplugin-auto-import, but I think not adding import { useTemplateRef } from 'vue' will break it for users who don't use unplugin-auto-import.
Could you change it to add import { useTemplateRef } from 'vue'?
Or could you change it to a suggestion instead of autofix?

Copy link
Contributor Author

@9romise 9romise Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I'll have a try.

@9romise 9romise marked this pull request as draft December 5, 2025 03:44
@FloEdelmann FloEdelmann changed the title feat(prefer-template-ref): support auto-fix feat(prefer-use-template-ref): support auto-fix Jan 16, 2026
@9romise
Copy link
Contributor Author

9romise commented Jan 18, 2026

Would it make sense to add an option to control whether imports are included during autofix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants